//
//  BLESDK.m
//  BLESDK
//
//  Created on Desktop.
//

#import "BLESDK.h"
#import "BLEManager.h"

// 错误域实现
NSString * const BLESDKErrorDomain = @"com.blesdk.error";

// 日志级别
typedef NS_ENUM(NSInteger, BLESDKLogLevel) {
    BLESDKLogLevelNone = 0,
    BLESDKLogLevelError = 1,
    BLESDKLogLevelWarning = 2,
    BLESDKLogLevelInfo = 3,
    BLESDKLogLevelDebug = 4
};

// 静态变量
static BOOL gDebugLogEnabled = NO;
static BLESDKLogLevel gLogLevel = BLESDKLogLevelError;
static NSDictionary *gSDKOptions = nil;

@implementation BLESDK

#pragma mark - Public Methods

+ (NSString *)version {
    return BLESDK_VERSION;
}

+ (void)initSDK {
    [self initSDKWithOptions:nil];
}

+ (void)initSDKWithOptions:(nullable NSDictionary *)options {
    // 保存配置选项
    if (options) {
        gSDKOptions = [options copy];
    } else {
        // 默认选项
        gSDKOptions = @{
            @"scanDuration": @(10.0),       // 默认扫描10秒
            @"autoReconnect": @(NO),         // 默认不自动重连
            @"connectionTimeout": @(5.0)    // 默认连接超时5秒
        };
    }
    
    // 获取BLEManager实例，确保在主线程初始化
    if ([NSThread isMainThread]) {
        [BLEManager sharedInstance];
    } else {
        dispatch_sync(dispatch_get_main_queue(), ^{
            [BLEManager sharedInstance];
        });
    }
    
    // 配置BLEManager
    [[BLEManager sharedInstance] applyOptions:gSDKOptions];
    
    [self logWithLevel:BLESDKLogLevelInfo message:@"BLESDK 初始化成功，版本：%@", [self version]];
}

+ (void)setDebugLogEnabled:(BOOL)enabled {
    gDebugLogEnabled = enabled;
    
    if (enabled) {
        gLogLevel = BLESDKLogLevelDebug;
        [self logWithLevel:BLESDKLogLevelInfo message:@"BLESDK 调试日志已启用"];
    } else {
        gLogLevel = BLESDKLogLevelError;
    }
}

+ (void)setLogLevel:(NSInteger)level {
    if (level >= BLESDKLogLevelNone && level <= BLESDKLogLevelDebug) {
        gLogLevel = (BLESDKLogLevel)level;
        [self logWithLevel:BLESDKLogLevelInfo message:@"BLESDK 日志级别已设置为: %ld", (long)level];
    } else {
        [self logWithLevel:BLESDKLogLevelWarning message:@"BLESDK 无效的日志级别: %ld", (long)level];
    }
}

+ (void)deinitSDK {
    // 断开连接并停止扫描
    [[BLEManager sharedInstance] disconnect];
    [[BLEManager sharedInstance] stopScan];
    
    [self logWithLevel:BLESDKLogLevelInfo message:@"BLESDK 已注销"];
}

#pragma mark - Private Methods

+ (void)logWithLevel:(BLESDKLogLevel)level message:(NSString *)format, ... {
    // 检查日志级别
    if (level > gLogLevel) {
        return;
    }
    
    // 如果没有启用调试日志并且级别不是错误，则跳过
    if (!gDebugLogEnabled && level != BLESDKLogLevelError) {
        return;
    }
    
    // 准备日志前缀
    NSString *prefix;
    switch (level) {
        case BLESDKLogLevelError:
            prefix = @"[BLESDK ERROR]";
            break;
        case BLESDKLogLevelWarning:
            prefix = @"[BLESDK WARNING]";
            break;
        case BLESDKLogLevelInfo:
            prefix = @"[BLESDK INFO]";
            break;
        case BLESDKLogLevelDebug:
            prefix = @"[BLESDK DEBUG]";
            break;
        default:
            prefix = @"[BLESDK]";
            break;
    }
    
    // 格式化日志消息
    va_list args;
    va_start(args, format);
    NSString *message = [[NSString alloc] initWithFormat:format arguments:args];
    va_end(args);
    
    // 输出日志
    NSLog(@"%@ %@", prefix, message);
}

@end 